import {Injectable} from '@angular/core';
import {Headers, RequestOptions} from '@angular/http';
import {adSystemApiUrl, appletApiUrl} from '../../../config/api';
import {HttpService} from '../../lib/service/http.service';
import {LocalStorageService} from "../../lib/service/local-storage.service";

@Injectable()
export class TemplateService {
  applet_uuid = this.localStorageService.getNowApplet()['applet_uuid'];
  user_uuid = this.localStorageService.getUserInfo()['user_uuid'];
  constructor(
    private httpService: HttpService,
    private localStorageService: LocalStorageService
  ) {
  }



  //修改模板状态
  toggleTemplateStatus(id, flag) {
    const url = `${adSystemApiUrl}/v1/member/goods_extend_template/set_default`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({id: id, default: flag,applet_id:this.applet_uuid,user_uuid:this.user_uuid});
    return this.httpService.httpPostAuth(url, body, options).map(res => res);
  }

  //获取模板信息
  getTemplateInfo(id) {
    const url  = `${adSystemApiUrl}/v1/member/goods_extend_template/template_info?id=${id}&applet_id=${this.applet_uuid}&user_uuid=${this.user_uuid}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  //添加模板信息
  addTemplateInfo(data) {
    const url = `${adSystemApiUrl}/v1/member/goods_extend_template/add_template`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    data['applet_id'] = this.applet_uuid;
    data['user_uuid'] = this.user_uuid;
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(res => res);
  }






  //添加活动
  addActivityPlan(data) {
    const url = `${adSystemApiUrl}/v3/member/activity_plan/add`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(res => res);
  }

  //修改活动
  editActivityPlan(data) {
    const url = `${adSystemApiUrl}/v3/member/activity_plan/edit`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(res => res);
  }

  //删除活动
  delActivityPlan(id) {
    const url = `${adSystemApiUrl}/v3/member/activity_plan/del`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({id: id});
    return this.httpService.httpPostAuth(url, body, options).map(res => res);
  }

  //活动详情
  getActivityInfo(id) {
    const url = `${adSystemApiUrl}/v3/member/activity_plan/info/?id=${id}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  //获取二维码
  getQrcode(id) {
    const url = `${adSystemApiUrl}/v3/member/activity_plan/get_activity_plan_erwei?id=${id}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }


  //编辑模板信息
  addOrEditTemplateInfo(data) {
    const url = `${appletApiUrl}/backend/attribute_template/` + data.url;
    return this.httpService.httpPostAuth(url, data).map(res => res);
  }

  //获取模板列表
  getTemplateList(shopid) {
    const url = `${appletApiUrl}/backend/attribute_template/index`;
    return this.httpService.httpPostAuth(url, {shop_id: shopid}).map(res => res);
  }

  deleteTemplate(data) {
    const url = `${appletApiUrl}/backend/attribute_template/` + data.url;
    return this.httpService.httpPostAuth(url, data).map(res => res);
  }

  addSpecName(data) {
    const url = `${appletApiUrl}/backend/attribute_template/attribute_create`;
    return this.httpService.httpPostAuth(url, data).map(res => res);
  }

}
